package com.bjb.vr.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjb.vr.common.ao.ResDevAuthorityAo;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.ResContentAuthority;
import com.bjb.vr.common.dto.ResDevAuthority;
import com.bjb.vr.common.dto.ResDevAuthorityDetail;
import com.bjb.vr.common.exception.CommonException;
import com.bjb.vr.common.mapper.ResDevAuthorityDetailMapper;
import com.bjb.vr.common.mapper.ResDevAuthorityMapper;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.ResDevAuthorityVo;
import com.bjb.vr.common.vo.UmsAccountVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
* @author admin
* @description 针对表【res_dev_authority(客户设备授权表)】的数据库操作Service
* @createDate 2022-12-12 09:36:54
*/
@Service
public class ResDevAuthorityService extends ServiceImpl<ResDevAuthorityMapper,ResDevAuthority> {


	@Autowired
	ResDevAuthorityMapper resDevAuthorityMapper;

	@Autowired
	ResDevAuthorityDetailMapper resDevAuthorityDetailMapper;

	@Autowired
	ResDevAuthorityDetailService resDevAuthorityDetailService;

	/**
	 * 查询客户设备授权列表
	 * @param resDevAuthorityAo
	 * @return
	 */
	public IPage<ResDevAuthorityVo> queryResDevAuthorityPage(ResDevAuthorityAo resDevAuthorityAo){
		long current = (resDevAuthorityAo.getCurrent() - 1) * resDevAuthorityAo.getSize();
		List<ResDevAuthorityVo> resDevAuthorityVoList = resDevAuthorityMapper.
				queryResDevAuthorityPage(resDevAuthorityAo.getTenantName(),current,resDevAuthorityAo.getSize());
		long total = resDevAuthorityMapper.queryResDevAuthorityTotal(resDevAuthorityAo.getTenantName());
		IPage<ResDevAuthorityVo> voIPage = new Page<ResDevAuthorityVo>(resDevAuthorityAo.getCurrent(), resDevAuthorityAo.getSize(), total);
		voIPage.setRecords(resDevAuthorityVoList);
		return voIPage;
	}


	/**
	 * 添加客户设备授权
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public boolean addResDevAuthority(ResDevAuthorityAo resDevAuthorityAo){
		Assert.notNull(resDevAuthorityAo.getTenantId(),"租户ID不能为空");
		Assert.notNull(resDevAuthorityAo.getUdids(),"设备不能为空");

		for(String udid:resDevAuthorityAo.getUdids()){
			LambdaQueryWrapper<ResDevAuthorityDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			lambdaQueryWrapper.eq(ResDevAuthorityDetail::getUdid,udid);
			ResDevAuthorityDetail resDevAuthorityDetail = resDevAuthorityDetailMapper.selectOne(lambdaQueryWrapper);
			if(resDevAuthorityDetail!=null){
				throw new CommonException("设备" + udid + "已存在授权,请先移除!");
			}
		}


		// 换成自定义租户ID
		UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
		umsAccountVo.setTempTenantId(resDevAuthorityAo.getTenantId());
		ResDevAuthority resDevAuthority = new ResDevAuthority();
		resDevAuthority.setTenantId(resDevAuthorityAo.getTenantId());
		resDevAuthority.setValidBegin(resDevAuthorityAo.getValidBegin());
		resDevAuthority.setValidEnd(resDevAuthorityAo.getValidEnd());
		resDevAuthority.setValidDays(resDevAuthorityAo.getValidDays());
		resDevAuthority.setGroupId(resDevAuthorityAo.getGroupId());
		boolean result = this.save(resDevAuthority);
		if(result) {
			List<ResDevAuthorityDetail> resDevAuthorityDetailList = new ArrayList<>();
			for (String udid : resDevAuthorityAo.getUdids()) {
				ResDevAuthorityDetail resDevAuthorityDetail = new ResDevAuthorityDetail();
				resDevAuthorityDetail.setResDevAuthorityId(resDevAuthority.getId());
				resDevAuthorityDetail.setTenantId(resDevAuthorityAo.getTenantId());
				resDevAuthorityDetail.setValidBegin(resDevAuthorityAo.getValidBegin());
				resDevAuthorityDetail.setValidEnd(resDevAuthorityAo.getValidEnd());
				resDevAuthorityDetail.setValidDays(resDevAuthorityAo.getValidDays());
				resDevAuthorityDetail.setUdid(udid);
				resDevAuthorityDetailList.add(resDevAuthorityDetail);
			}
			result =  resDevAuthorityDetailService.saveBatch(resDevAuthorityDetailList);

		}
		return result;
	}


	/**
	 * 修改客户设备授权
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public  boolean updateResDevAuthority(ResDevAuthorityAo resDevAuthorityAo){
		Assert.notNull(resDevAuthorityAo.getId(),"设备授权ID不能为空");
		Assert.notNull(resDevAuthorityAo.getTenantId(),"租户ID不能为空");
		Assert.notNull(resDevAuthorityAo.getUdids(),"设备不能为空");

		boolean result = false;
		// 换成自定义租户ID
		UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
		umsAccountVo.setTempTenantId(resDevAuthorityAo.getTenantId());
		LambdaQueryWrapper<ResDevAuthorityDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(ResDevAuthorityDetail::getResDevAuthorityId,resDevAuthorityAo.getId());
		int count = resDevAuthorityDetailMapper.delete(lambdaQueryWrapper);
		if(count==0){
			umsAccountVo.setTempTenantId("");
			return result;
		}

		for(String udid:resDevAuthorityAo.getUdids()){
			LambdaQueryWrapper<ResDevAuthorityDetail> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
			lambdaQueryWrapper1.eq(ResDevAuthorityDetail::getUdid,udid);
			ResDevAuthorityDetail resDevAuthorityDetail = resDevAuthorityDetailMapper.selectOne(lambdaQueryWrapper1);
			if(resDevAuthorityDetail!=null){
				throw new CommonException("设备" + udid + "已存在授权,请先移除!");
			}
		}

		ResDevAuthority resDevAuthority = new ResDevAuthority();
		resDevAuthority.setId(resDevAuthorityAo.getId());
		resDevAuthority.setTenantId(resDevAuthorityAo.getTenantId());
		resDevAuthority.setValidBegin(resDevAuthorityAo.getValidBegin());
		resDevAuthority.setValidEnd(resDevAuthorityAo.getValidEnd());
		resDevAuthority.setValidDays(resDevAuthorityAo.getValidDays());
		resDevAuthority.setGroupId(resDevAuthorityAo.getGroupId());
		result = this.updateById(resDevAuthority);

		if(!result){
			umsAccountVo.setTempTenantId("");
			return result;
		}

		List<ResDevAuthorityDetail> resDevAuthorityDetailList = new ArrayList<>();
		for(String udid:resDevAuthorityAo.getUdids()){
			ResDevAuthorityDetail resDevAuthorityDetail = new ResDevAuthorityDetail();
			resDevAuthorityDetail.setResDevAuthorityId(resDevAuthority.getId());
			resDevAuthorityDetail.setTenantId(resDevAuthorityAo.getTenantId());
			resDevAuthorityDetail.setValidBegin(resDevAuthorityAo.getValidBegin());
			resDevAuthorityDetail.setValidEnd(resDevAuthorityAo.getValidEnd());
			resDevAuthorityDetail.setValidDays(resDevAuthorityAo.getValidDays());
			resDevAuthorityDetail.setUdid(udid);
			resDevAuthorityDetailList.add(resDevAuthorityDetail);
		}
		result = resDevAuthorityDetailService.saveBatch(resDevAuthorityDetailList);
		umsAccountVo.setTempTenantId("");
		return result;
	}

	/**
	 * 删除客户设备授权
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public  boolean deleteResDevAuthority(ResDevAuthorityAo resDevAuthorityAo){
		return false;
	}

	/**
	 * 查询设备授权详情
	 * @param resDevAuthorityAo
	 * @return
	 */
	public ResDevAuthorityVo queryResDevAuthorityDetail(ResDevAuthorityAo resDevAuthorityAo){
		Assert.notNull(resDevAuthorityAo.getId(),"设备授权ID不能为空");
		ResDevAuthorityVo resDevAuthorityVo = new ResDevAuthorityVo();
		ResDevAuthority resDevAuthority = new ResDevAuthority();
		resDevAuthority = this.getById(resDevAuthorityAo.getId());

		BeanUtils.copyProperties(resDevAuthority,resDevAuthorityVo);

		LambdaQueryWrapper<ResDevAuthorityDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(ResDevAuthorityDetail::getResDevAuthorityId,resDevAuthorityAo.getId());
		List<ResDevAuthorityDetail> resDevAuthorityDetailList = resDevAuthorityDetailMapper.selectList(lambdaQueryWrapper);
		if(resDevAuthorityDetailList!=null && resDevAuthorityDetailList.size()>0){
			List<String> udidList = new ArrayList<>();
			for(ResDevAuthorityDetail dev :resDevAuthorityDetailList){
				udidList.add(dev.getUdid());
			}
			resDevAuthorityVo.setUdidList(udidList);
		}
		return resDevAuthorityVo;
	}

}
